/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.attribute;

/**
 * Provides an Apache Commons attribute allowing business object
 * bind properties to be specified as field meta-data.
 * 
 * <p>
 * This class overrides {@link #hashCode()} and {@link #equals(Object)} to
 * guarantee uniqueness.  Only one business object property can be
 * assigned per field.  See Apache Commons
 * <a href="http://commons.apache.org/attributes/declaring.html">Attributes documentation</a>
 * for the justifcation for overriding these methods.
 * </p>
 *   
 * @author Scott Rossillo
 * 
 * @see net.martinimix.beans.bind.AttributeAwareFieldPropertyMappingHandler
 * 
 * @@org.apache.commons.attributes.Target(org.apache.commons.attributes.Target.FIELD)
 * @@org.apache.commons.attributes.Inheritable()
 */
public class BusinessObjectProperty {
	
	private String name;
	private boolean attribute;
	
	/**
	 * Creates a new business object property for a field whose value
	 * will be bound to the given business object property name.
	 * 
	 * @param name the name of the business object property the field
	 * should be bound to
	 * 
	 */
	public BusinessObjectProperty(String name) {
		this.name = name;
		this.attribute = false;
	}
	
	/**
	 * Returns true if this property represents a Blue Martini attribute.
	 * 
	 * @return <code>true</code> if this property represents a business object
	 * attribute; <code>false</code> otherwise
	 */
	public boolean isAttribute() {
		return attribute;
	}
	
	/**
	 * True if this property represents a Blue Martini attribute.
	 * 
	 * @param attribute <code>true</code> if this property represents a business object
	 * attribute; <code>false</code> otherwise
	 */
	public void setAttribute(boolean attribute) {
		this.attribute = attribute;
	}
	
	/**
	 * Returns the fully qualified name of this property.
	 * 
	 * @return the fully qualified name of this property
	 */
	public String getFullyQualifiedName() {
		
		return attribute ? "ATR_" + name : name;
	}
	
	/**
	 * Returns the name of this business object property.
	 * 
	 * @return the name of this business object property
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Returns a hash code of zero to guarantee uniqueness.
	 * 
	 * <a href="http://commons.apache.org/attributes/declaring.html">
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode () { 
		return 0; 
	}
	
    /**
     * Returns true if the given object is an instance of
     * <code>BusinessObjectProperty</code>
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals (Object o) { 
    	return o instanceof BusinessObjectProperty; 
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
    	return this.getFullyQualifiedName();
    }
	
}
